import 'dart:convert';

T? asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }
  return null;
}

class Result<T> {
  Result({
    this.code,
    this.data,
    this.msg,
  });

  factory Result.fromJson(Map<String, dynamic> jsonRes) => Result(
        code: asT<int?>(jsonRes['code']),
        data: asT<T?>(jsonRes['data']),
        msg: asT<String?>(jsonRes['msg']),
      );

  int? code;
  T? data;
  String? msg;

  @override
  String toString() {
    return jsonEncode(this);
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'code': code,
        'data': data,
        'msg': msg,
      };

  Result clone() => Result.fromJson(asT<Map<String, dynamic>>(jsonDecode(jsonEncode(this)))!);
}
